package cn.iocoder.yudao.module.system.controller.admin.gugu;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.system.controller.admin.gugu.vo.CollegeScoreCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.gugu.vo.CollegeScorePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.gugu.vo.CollegeScoreRespVO;
import cn.iocoder.yudao.module.system.controller.admin.gugu.vo.CollegeScoreUpdateReqVO;
import cn.iocoder.yudao.module.system.convert.gugu.CollegeScoreConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.gugu.CollegeScoreDO;
import cn.iocoder.yudao.module.system.service.gugu.CollegeScoreService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 历年高考高校录取分数线数据 Controller
 */
@Tag(name = "管理后台 - 历年高考高校录取分数线数据")
@RestController
@RequestMapping("/system/college-score")
@Validated
@Slf4j
public class CollegeScoreController {

    @Resource
    private CollegeScoreService collegeScoreService;

    @PostMapping("/create")
    @Operation(summary = "创建高校录取分数线数据")
    @PermitAll
    public CommonResult<Long> createCollegeScore(@Valid @RequestBody CollegeScoreCreateReqVO createReqVO) {
        return success(collegeScoreService.createCollegeScore(BeanUtils.toBean(createReqVO, CollegeScoreDO.class)));
    }

    @PutMapping("/update")
    @Operation(summary = "更新高校录取分数线数据")
    @PermitAll
    public CommonResult<Boolean> updateCollegeScore(@Valid @RequestBody CollegeScoreUpdateReqVO updateReqVO) {
        collegeScoreService.updateCollegeScore(BeanUtils.toBean(updateReqVO, CollegeScoreDO.class));
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除高校录取分数线数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PermitAll
    public CommonResult<Boolean> deleteCollegeScore(@RequestParam("id") Long id) {
        collegeScoreService.deleteCollegeScore(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得高校录取分数线数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PermitAll
    public CommonResult<CollegeScoreRespVO> getCollegeScore(@RequestParam("id") Long id) {
        CollegeScoreDO collegeScore = collegeScoreService.getCollegeScore(id);
        return success(BeanUtils.toBean(collegeScore, CollegeScoreRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得高校录取分数线数据分页")
    @PermitAll
    public CommonResult<PageResult<CollegeScoreRespVO>> getCollegeScorePage(@Valid CollegeScorePageReqVO pageReqVO) {
        PageResult<CollegeScoreDO> pageResult = collegeScoreService.getCollegeScorePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CollegeScoreRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出高校录取分数线数据 Excel")
    @PermitAll
    public void exportCollegeScoreExcel(@Valid CollegeScorePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CollegeScoreDO> list = collegeScoreService.getCollegeScorePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "高校录取分数线数据.xls", "数据", CollegeScoreRespVO.class,
                        BeanUtils.toBean(list, CollegeScoreRespVO.class));
    }

    @GetMapping("/fetch")
    @Operation(summary = "从API获取并保存高校录取分数线数据")
    @Parameter(name = "appkey", description = "API密钥", required = true)
    @Parameter(name = "searchtype", description = "查询条件类型，PROVINCENAME 或 COLLEGENAME", required = true)
    @Parameter(name = "keyword", description = "录取省份或高校名称", required = true)
    @Parameter(name = "year", description = "查询的录取年份", required = false)
    @Parameter(name = "pageindex", description = "页码", required = false)
    @Parameter(name = "pagesize", description = "每页大小", required = false)
    @PermitAll
    public CommonResult<Map<String, Object>> fetchCollegeScore(
            @RequestParam("appkey") String appkey,
            @RequestParam("searchtype") String searchtype,
            @RequestParam("keyword") String keyword,
            @RequestParam(value = "year", required = false) Integer year,
            @RequestParam(value = "pageindex", required = false, defaultValue = "1") Integer pageindex,
            @RequestParam(value = "pagesize", required = false, defaultValue = "10") Integer pagesize) {
        
        Map<String, Object> result = collegeScoreService.fetchAndSaveCollegeScore(appkey, searchtype, keyword, year, pageindex, pagesize);
        return success(result);
    }

    @GetMapping("/list")
    @Operation(summary = "查询高校录取分数线数据列表")
    @Parameter(name = "province", description = "省份", required = false)
    @Parameter(name = "year", description = "年份", required = false)
    @Parameter(name = "collegeName", description = "高校名称", required = false)
    @PermitAll
    public CommonResult<List<CollegeScoreRespVO>> getCollegeScoreList(
            @RequestParam(value = "province", required = false) String province,
            @RequestParam(value = "year", required = false) Integer year,
            @RequestParam(value = "collegeName", required = false) String collegeName) {
        
        List<CollegeScoreDO> list = collegeScoreService.getCollegeScoreList(province, year, collegeName);
        return success(BeanUtils.toBean(list, CollegeScoreRespVO.class));
    }

    @DeleteMapping("/delete-by-condition")
    @Operation(summary = "根据条件删除高校录取分数线数据")
    @Parameter(name = "province", description = "省份", required = true)
    @Parameter(name = "year", description = "年份", required = true)
    @Parameter(name = "schoolUuid", description = "高校UUID", required = true)
    @Parameter(name = "admissionBatch", description = "录取批次", required = false)
    @Parameter(name = "typeName", description = "文科理科类型", required = false)
    @PermitAll
    public CommonResult<Integer> deleteCollegeScoreByCondition(
            @RequestParam("province") String province,
            @RequestParam("year") Integer year,
            @RequestParam("schoolUuid") String schoolUuid,
            @RequestParam(value = "admissionBatch", required = false) String admissionBatch,
            @RequestParam(value = "typeName", required = false) String typeName) {
        
        int deletedCount = collegeScoreService.deleteCollegeScore(province, year, schoolUuid, admissionBatch, typeName);
        return success(deletedCount);
    }

    @DeleteMapping("/delete-by-year")
    @Operation(summary = "根据年份删除高校录取分数线数据")
    @Parameter(name = "year", description = "年份", required = true)
    @PermitAll
    public CommonResult<Integer> deleteCollegeScoreByYear(@RequestParam("year") Integer year) {
        int deletedCount = collegeScoreService.deleteCollegeScoreByYear(year);
        return success(deletedCount);
    }
}
